﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ABCLogistic_System_V2._0.DA;

namespace ABCLogistic_System_V2._0.GUI
{
    public partial class frmQTHT_Nhomnguoidung : Form
    {
        ABCLogisticsEntities context = new ABCLogisticsEntities();
        NHOMNGUOIDUNG nhomnguoidung = new NHOMNGUOIDUNG();
        NhomQuyen nhomquyen = new NhomQuyen();
        public delegate void LoadNhom(bool bchecknhom);
        public event LoadNhom nhomok;

        public frmQTHT_Nhomnguoidung()
        {
            InitializeComponent();
        }

        /// <summary>
        /// xu ly button Luu - Luu nhom
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLuu_Click(object sender, EventArgs e)
        {
            nhomnguoidung.MANHOM = "";
            nhomnguoidung.TENNHOM = cbxtennhom.Text;
            nhomnguoidung.GHICHU = txtmotanhom.Text;

            if (cbxtennhom.Text=="")
            {
                MessageBox.Show("Bạn chưa nhập tên nhóm!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //// luu
                //context.NHOMNGUOIDUNGs.AddObject(nhomnguoidung);
                int? resulttext= context.SP_NHOMNGUOIDUNG_INSERT("", cbxtennhom.Text, txtmotanhom.Text).SingleOrDefault();
                //int count = context.SaveChanges();
                if (resulttext == 1)
                {
                    DialogResult result=MessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    if (result == DialogResult.OK)
                    {
                        if (nhomok != null)
                        {
                            this.nhomok(true);
                        }
                        // xet lai cac gia tri trong textbox
                        cbxtennhom.Text = "";
                        txtmotanhom.Text = "";
                        cbxtennhom.DropDownStyle = ComboBoxStyle.DropDownList;
                    }
                        
                }
                else
                {
                    MessageBox.Show("Không thể lưu!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            LoadNhomNguoiDung();
            cbxtennhom.Text = "";
            txtmotanhom.Text = "";
        }

        private void LoadNhomNguoiDung()
        {
            var nhomnguoidung = from cat in context.NHOMNGUOIDUNGs
                                select cat;
            cbxtennhom.DataSource = nhomnguoidung.ToList<NHOMNGUOIDUNG>();
            cbxtennhom.DisplayMember = "TENNHOM";
            cbxtennhom.ValueMember = "MANHOM";
        }

        private void btnthemnhom_Click(object sender, EventArgs e)
        {
            if (cbxtennhom.Text != "")
            {
                DialogResult result = MessageBox.Show("Bạn có muốn lưu không!", "Thông báo", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (DialogResult.OK == result)
                {
                    btnLuu_Click(sender, e);
                }
                else
                {
                    if (DialogResult.No == result)
                    {
                        frmQTHT_Nhomnguoidung_Load(sender, e);
                    }
                }
            }
            
        }

        private void btnchinhsua_Click(object sender, EventArgs e)
        {
            cbxtennhom.DropDownStyle = ComboBoxStyle.DropDown;
            LoadNhomNguoiDung();
            nhomnguoidung = (from p in context.NHOMNGUOIDUNGs
                        where p.TENNHOM == cbxtennhom.Text
                        select p).FirstOrDefault<NHOMNGUOIDUNG>();
            if (nhomnguoidung != null)
            {
                txtmotanhom.Text = nhomnguoidung.GHICHU;
            }
        }

        private void cbxtennhom_SelectedValueChanged(object sender, EventArgs e)
        {
            nhomnguoidung = (from p in context.NHOMNGUOIDUNGs
                             where p.TENNHOM == cbxtennhom.Text
                             select p).FirstOrDefault<NHOMNGUOIDUNG>();
            if (nhomnguoidung != null)
            {
                txtmotanhom.Text = nhomnguoidung.GHICHU;
            }
        }

        private void frmQTHT_Nhomnguoidung_Load(object sender, EventArgs e)
        {

        }
    }
}
